package com.eqdd.yiqidian.ui.mine.expense;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.CoolRefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ExpenseBean;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.http.ListHttpResult;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by lvzhihao on 17-7-19.
 */

public class CheckExpenseFragment extends BaseFragment {
    CoolRefreshRecyclerViewFragmentCustom dataBinding;
    private SlimAdapterEx<ExpenseBean> slimAdapterEx;
    private int status;
    private ModelRx2DataSource<ExpenseBean> dataSource;
    private MVCCoolHelper<List<ExpenseBean>> mvcHelper;
    private int pageNum = 0;

    @Override
    protected void setView() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_123, new SlimInjector<ExpenseBean>() {
                @Override
                public void onInject(ExpenseBean data, IViewInjector injector) {
                    injector.text(R.id.tv_type, data.getRemibursetitle())
                            .text(R.id.tv_checker, data.getCreateName())
                            .text(R.id.tv_time, data.getCreateTime());
                }
            })
                    .attachTo(dataBinding.recyclerView);
            dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
                Bundle bundle = new Bundle();
                bundle.putString("expenseId", slimAdapterEx.getData().get(position).getId() + "");
                bundle.putInt("status", status);
                startActivity(CheckExpenseInfoActivity.class, bundle);
            });
            User user = GreenDaoUtil.getUser();

            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    pageNum = page == 1 ? 0 : pageNum;
                    return OkGo.<ListHttpResult<ExpenseBean>>post(HttpConfig.BASE_URL + HttpConfig.REIMBURSE_LIST_CHECKER)
                            .params("page", pageNum)
                            .params("companyId", user.getCompanyId())
                            .params("userGuid", user.getGuid())
                            .params("type", status)
                            .converter(new JsonConverter<ListHttpResult<ExpenseBean>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(noticeWraperResultBeanHttpResult -> {
                                pageNum = noticeWraperResultBeanHttpResult.getPage();
                                return Flowable.just(noticeWraperResultBeanHttpResult.getItems() == null ? new ArrayList<ExpenseBean>() : noticeWraperResultBeanHttpResult.getItems());
                            });

                }
            }, 10);
            slimAdapterEx.updateData(new ArrayList<ExpenseBean>());
            mvcHelper = new MVCCoolHelper<List<ExpenseBean>>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    protected void initData() {

        status = getArguments().getInt("status");
    }


    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_cool_recycler_view, null, false);

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            if (requestCode == Config.APPLY_TRAVEL) {
                mvcHelper.refresh();
            }
        }
    }
}
